package com.macro.mall.bo;

import com.macro.mall.model.UmsAdmin;
import com.macro.mall.model.UmsResource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/** SpringSecurity需要的用户信息封装类 Created by macro on 2018/4/26. */
public class AdminUserDetails implements UserDetails {
  // 后台用户
  private final UmsAdmin umsAdmin;
  // 拥有资源列表
  private final List<UmsResource> resourceList;

  public AdminUserDetails(UmsAdmin umsAdmin, List<UmsResource> resourceList) {
    this.umsAdmin = umsAdmin;
    this.resourceList = resourceList;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    // 返回当前用户所拥有的资源
    return resourceList.stream()
        .map(resource -> new SimpleGrantedAuthority(resource.getId() + ":" + resource.getName()))
        .collect(Collectors.toList());
  }

  @Override
  public String getPassword() {
    return umsAdmin.getPassword();
  }

  @Override
  public String getUsername() {
    return umsAdmin.getUsername();
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return umsAdmin.getStatus().equals(1);
  }
}
